Day 14 - Regular expressions - Classes

46

$ cat examples.txt | grep -E "[a-zA-Z]"

matches any letter, but not digits. Pay attention that a range like A-z (uppercase A and lowercase

z) works but will match also other characters that are encoded between uppercase and lowercase

letters, namely [, \, ], ^, _, and “‘. If your intention is that of including those characters you might

want to explicitly add them to the class, as the A-z syntax might be easily overlooked. Be kind to

whoever will have to debug your code, the Unix terminal is already more cryptic than ancient tomes

of magic, don’t make it worse.

Remember that a class, that is the whole block between square brackets, brackets included, is just

an element of the regular expression, and can be followed or preceded by other elements. Exercises

2 and 3 will help you to see this in practice.

Exercises

Exercise 14.07

Match any line of examples.txt containing a digit

Go to solution

Exercise 14.08

Match any line of examples.txt containing a lowercase “a” followed by any letter (that is “aa”, “ab”,

“ac”, and so on)

Go to solution

Exercise 14.09

Match any line of examples.txt containing an upper case letter followed by a digit

Go to solution

Exercise 14.10

Match any line of examples.txt containing a dash

Go to solution